package com.zc.mychart.chart;

import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.zc.mychart.R;
import com.zc.mychart.model.KTimeType;
import com.zc.mychart.utils.DecimalUtil;

/**
 * Created by zhangchi on 2018/3/7.
 */

public class MarkerViewRightY extends MarkerView {
    private TextView tv_content;
    private MPPointF mOffset = new MPPointF();
    private double mMid;

    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    public MarkerViewRightY(Context context, int layoutResource) {
        super(context, layoutResource);
        tv_content = (TextView) findViewById(R.id.tv_content);
    }

    public void setMarkerBg(int resId, int textColor) {
        tv_content.setBackgroundResource(resId);
        tv_content.setTextColor(textColor);
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        String content = (DecimalUtil.div((e.getY() - mMid) * 100, mMid, 2)) + "%";
        if (e.getY() - mMid == 0) {
            content = "0.00%";
        }
        if(MyLineChart.mKTimeType == KTimeType.MIN1){
            tv_content.setText(content);
        }else{
            tv_content.setText("");
        }
        super.refreshContent(e, highlight);
    }

    public void setMidValue(double value) {
        mMid = value;
    }

    @Override
    public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) {
        Chart chart = getChartView();
        if (chart == null)
            return mOffset;
        float width = getWidth();
        float height = getHeight();
        mOffset.x = chart.getWidth() - posX - width;
        mOffset.y = -height / 2.0f;
        if (posY + mOffset.y < 0) {
            mOffset.y = -posY;
        } else if (posY + height + mOffset.y > chart.getHeight()) {
            mOffset.y = chart.getHeight() - posY - height;
        }
        return mOffset;
    }
}
